package dongbai.soft.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import dongbai.soft.entity.ProductEntity;
import dongbai.soft.entity.TypeEntity;
import dongbai.soft.service.ProductService;
import dongbai.soft.service.TypeService;
import dongbai.soft.utils.Constant;
import dongbai.soft.utils.PageUtils;
import dongbai.soft.utils.R;
import dongbai.soft.utils.getUUID;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;


/**
 * @author SingDeer
 * @email
 * @date 2023-06-26 14:01:21
 */
@RestController
@RequestMapping("/type")
public class TypeController {
    @Autowired
    private TypeService typeService;

    @Autowired
    private ProductService productService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions(":type:list")
    public R list(@RequestBody Map<String, Object> params) {
        PageUtils page = typeService.queryPage(params);
        if (!StringUtils.isEmpty((String) params.get("name"))) {
            List<TypeEntity> typeEntities = (List<TypeEntity>) page.getList();
            Set<TypeEntity> children = new HashSet<>();
            for (TypeEntity typeEntity : typeEntities) {
                if (typeEntity.getParentid().equals("0")) {
                    children.addAll(typeService.list(new QueryWrapper<TypeEntity>()
                            .eq("parentID", typeEntity.getTid())));
                }
            }
            children.addAll(typeEntities);
            page.setList(new ArrayList<>(children));
        }

        return R.ok().put("page", page);
    }

    /**
     * 查询二级类别及其父类别
     */
    @RequestMapping("/info")
    //@RequiresPermissions(":type:list")
    public R info(@RequestBody Map<String, String> params) {
        String TID = params.get("TID");
        String FTID = typeService.getById(TID).getParentid();
        return Objects.requireNonNull(R.ok().put("ftname", typeService.getById(FTID).getName())).put("stname", typeService.getById(TID).getName());
    }


    /**
     * 新增
     */
    @RequestMapping("/new")
    public R save(@RequestBody TypeEntity type) {
        if (typeService.DUP(type.getName())) {
            return R.error(Constant.TYPE_DUP);
        }
        type.setTid(getUUID.get6UUID());
        type.setStatus(Constant.STATE_Y);
        typeService.save(type);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/editinfo")
    public R update(@RequestBody Map<String, String> maps) {
        if (typeService.DUP(maps.get("name"))) {
            return R.error(Constant.TYPE_DUP);
        }

        typeService.update(new UpdateWrapper<TypeEntity>().eq("TID", maps.get("TID")).set("name", maps.get("name")));

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Map<String, String> maps) {
        if (productService.getOne(new QueryWrapper<ProductEntity>().eq("TID", maps.get("TID")).eq("status", Constant.STATE_Y)) != null) {
            return R.error(Constant.TDEL_FAILD);
        }
        typeService.update(new UpdateWrapper<TypeEntity>().eq("TID", maps.get("TID")).set("status", Constant.STATE_N));

        return R.ok();
    }

}
